<html>
<head>
<script type="text/javascript">

// Saves options to localStorage.
function load_staus() {
	var s="";
	var back_object;
	var page_object;
	chrome.extension.sendRequest({method: "read_settings"}, function(back_response) {
		back_object = back_response.settings_object;
		chrome.tabs.getSelected(null, function (tab) {
			//console.log(tab);
			chrome.tabs.sendRequest(tab.id, {method: "get_page_data"}, function(page_response) {
				page_object = page_response.returningobject;
			  Proceed(back_object, page_object);
			});
		});
		
	});

}


function Proceed (back_object, page_object) {

	document.getElementById('status_menu');
	if(page_object.thread) {
		status_menu.innerHTML = "";
		status_menu.innerHTML += "Thread: "+page_object.thread+"<br />";
		status_menu.innerHTML += "In Game: "+page_object.gameTag+"<br />";
		
		if (page_object.cast) {
			//console.log("back_object:");
			//console.log(back_object);
			//console.log("page_object:");
			//console.log(page_object);
			//console.log("page_object.cast:");
			//console.log(page_object.cast);
			for (player in page_object.cast.players) {
				//console.log(page_object.cast.players[player]);
				
				if (page_object.cast.players[player].character && page_object.cast.players[player].found) {
					//console.log(page_object.cast.players[player]);
					status_menu.innerHTML += '<a href="player_popup.html?player='+player+'&gameTag='+escape(page_object.gameTag)+'">[edit]</a>';
					status_menu.innerHTML += '<a style="color:#008800" target="_new" href="http://online-roleplaying.com/forums/member.php?u='+player+'">'+page_object.cast.players[player].character.character_tag+' - '+page_object.cast.players[player].user+'</a><br />';
				} else if (page_object.cast.players[player].character) {
					status_menu.innerHTML += '<a href="player_popup.html?player='+player+'&gameTag='+escape(page_object.gameTag)+'">[edit]</a>';
					status_menu.innerHTML += '<a style="color:#888800" target="_new" href="http://online-roleplaying.com/forums/member.php?u='+player+'">'+page_object.cast.players[player].character.character_tag+'</a><br />';
				} else {
					status_menu.innerHTML += '<a href="#" onclick="add_player(\''+player+'\',\''+escape(page_object.gameTag)+'\')">[add]</a>';
					status_menu.innerHTML += '<a style="color:#880000" target="_new" href="http://online-roleplaying.com/forums/member.php?u='+player+'">'+page_object.cast.players[player].user+'</a><br />';
				}
			}
		}
		
	}

}

function add_player(player,game) {
	chrome.extension.sendRequest({method: "add_player", player: player, game: game}, function(response) {
		if (response.result == true) {
			alert("Player "+player+" added to \""+game+"\"");
		} else {
			alert("Player not added.");
		}
	});
};

</script>
</head>
<body onload="load_staus()" style="padding:16px;font-family: verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;font-size: 16px; background-color:#3C3027;">
<div style="min-width:300px; min-height:200px; border: 1px solid black; background-color: white;">
<span id="status_menu">Status Not Loaded</span><br />
<br />
<a href="help.html">Help</a> | <a href="options.html" target="_new">Options</a>
</div>
</body>
</html>
